<template>
  <ContentWrap>
    <!-- 搜索栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="90px"
    >
      <el-form-item label="素材名称" prop="metaName">
        <el-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          placeholder="请输入素材名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['system:role:create']"
          plain
          type="primary"
          @click="openImageUpload"
        >
          <Icon class="mr-5px" icon="ep:upload" />
          上传
        </el-button>
        <el-button
          v-hasPermi="['system:role:create']"
          plain
          type="primary"
          @click="openImageUpload"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新建文件夹
        </el-button>
        <el-button
          v-hasPermi="['system:role:create']"
          plain
          type="primary"
          @click="openImageUpload"
        >
          <Icon class="mr-5px" icon="codicon:move" />
          移动
        </el-button>
        <el-button
          v-hasPermi="['system:role:export']"
          :loading="exportLoading"
          plain
          type="danger"
          @click="handleExport"
        >
          <Icon class="mr-5px" icon="ep:delete" />
          删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 图片列表 -->
  <ContentWrap>
    <div class="demo-image__error">
      <div class="block" v-for="(item, index) in list" :key="item">
        <span class="checkbox">
          <el-checkbox />
          <Icon class="option" icon="ep:edit" />
        </span>
        <el-image
          fit="contain"
          :src="item.previewUrl"
          :preview-src-list="previewList"
          :initial-index="initPreview"
          :zoom-rate="1.2"
          :max-scale="7"
          :min-scale="0.2"
          @show="previewShow(index)"
        />
        <span class="demonstration">{{ item.name }}</span>
        <span class="datetime">{{ item.createTime }}</span>
      </div>
    </div>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <ImageUpload ref="imageUploadRef" />
</template>

<script lang="ts" setup>
import { ref, reactive, defineComponent } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import * as resourceApi from '@/api/resource'
import ImageUpload from '@/views/common/ImageUpload.vue'

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const previewList = ref([])
const initPreview = ref(0)
const showImageUploadDialogFlag = ref(false)
const imageList = ref([])

const imageUploadRef = ref<any>()

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '',
  createTime: []
})
const previewShow = (index) => {
  initPreview.value = index
}
const openImageUpload = () => {
  imageUploadRef.value.openImageUpload()
}
/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await resourceApi.getPersonalImagePage(queryParams)
    list.value = data.list
    total.value = data.total
    previewList.value = []
    list.value.forEach((key, index) => {
      previewList.value.push(key.previewUrl)
    })
  } finally {
    loading.value = false
  }
}
const defineImageUpload = (data) => {
  imageList.value = data
}
const saveImage = () => {
  console.log(imageList.value)
}

provide('getList', getList)

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scoped>
.body {
  margin: 0;
}

.demo-image__error .block {
  display: inline-block;
  width: 20%;
  height: 330px;
  padding: 30px 20px;
  text-align: center;
  vertical-align: top;
  border-right: solid 1px var(--el-border-color);
  box-sizing: border-box;
}

.demo-image__error .block .more {
  float: right;
  color: #8c8a8a;
}

.demo-image__error .demonstration {
  display: block;
  margin-top: 10px;
  font-size: 14px;
  color: #3d3d3d;
  text-align: left;
}

.demo-image__error .datetime {
  display: block;
  margin-top: 10px;
  font-size: 14px;
  color: #666;
  text-align: left;
}

.demo-image__error .checkbox {
  display: block;
  margin-bottom: 10px;
  font-size: 14px;
  text-align: left;
}

.demo-image__error .el-image {
  width: 100%;
  height: 100%;
  max-width: 300px;
  max-height: 200px;
}

.demo-image__error .image-slot {
  display: flex;
  width: 100%;
  height: 100%;
  font-size: 30px;
  color: var(--el-text-color-secondary);
  background: var(--el-fill-color-light);
  justify-content: center;
  align-items: center;
}

.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}

.option {
  display: flex;
  float: right;
  color: #8c8e91;
  cursor: pointer;
  align-items: center;
}
</style>
